/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.gift.service;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.boxin.ims.modules.gift.entity.GiftCustomer;
import com.boxin.ims.modules.gift.dao.GiftCustomerDao;

/**
 * 礼品客户Service
 * @author Jakemanse
 * @version 2013-07-28
 */
@Component
@Transactional(readOnly = true)
public class GiftCustomerService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(GiftCustomerService.class);
	
	@Autowired
	private GiftCustomerDao giftCustomerDao;
	
	public GiftCustomer get(Long id) {
		return giftCustomerDao.findOne(id);
	}
	
	public Page<GiftCustomer> find(Page<GiftCustomer> page, GiftCustomer giftCustomer) {
		DetachedCriteria dc = giftCustomerDao.createDetachedCriteria();
		if (giftCustomer.getUser()!=null && giftCustomer.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", giftCustomer.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(giftCustomer.getName())){
			dc.add(Restrictions.like("name", "%"+giftCustomer.getName()+"%"));
		}
		if (StringUtils.isNotEmpty(giftCustomer.getRemarks())){
			dc.add(Restrictions.like("remarks", "%"+giftCustomer.getRemarks()+"%"));
		}
		dc.add(Restrictions.eq("delFlag", GiftCustomer.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return giftCustomerDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(GiftCustomer giftCustomer) {
		giftCustomerDao.save(giftCustomer);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		giftCustomerDao.deleteById(id);
	}
	
}
